package com.ymt.bpm.web.rest.platform.fauth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ymt.bpm.model.PRole;
import com.ymt.bpm.service.platform.fauth.FaRoleService;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.EncodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/3/19.
 */
@RestController
public class FaRoleResource {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FaRoleService rs;

    @RequestMapping("/api/v1/fa/role/query")
    public JSONObject query(HttpServletRequest req,
                                  @RequestParam(name="roleType", required = false)String roleType,
                                  @RequestParam(name="search", required = false)String search,
                                  @RequestParam(name="sort", required = false)String sort,
                                  @RequestParam(name="order", required = false)String order,
                                  @RequestParam("offset")int offset,
                                  @RequestParam("limit")int limit,
                                  @RequestParam("timezoneOffset")int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (search!=null && search.length()>0) {
            search = EncodeUtil.decode(search);
        }
        if (log.isDebugEnabled()) {
            log.debug("query tenantId=" + tenantId);
            log.debug("query roleType=" + roleType);
            log.debug("query search=" + search);
            log.debug("query sort=" + sort);
            log.debug("query order=" + order);
            log.debug("query offset=" + offset);
            log.debug("query limit=" + limit);
            log.debug("query timezoneOffset=" + timezoneOffset);
        }
        return rs.query(tenantId, roleType, search, sort, order, offset, limit, timezoneOffset);
    }

    @RequestMapping(value="/api/v1/fa/role/{roleId}", method = RequestMethod.GET)
    public JSONObject get(HttpServletRequest req, @PathVariable("roleId")String roleId,
                          @RequestParam(name="orgId",required = false)String orgId,
                          @RequestParam(name="timezoneOffset",required = true)int timezoneOffset) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("get tenantId=" + tenantId);
            log.debug("get orgId=" + orgId);
            log.debug("get roleId=" + roleId);
            log.debug("get timezoneOffset=" + timezoneOffset);
        }
        return rs.get(tenantId, orgId, roleId, lang, timezoneOffset);
    }

    @RequestMapping(value="/api/v1/fa/role/save", method = RequestMethod.POST)
    public JSONObject save(HttpServletRequest req, PRole role) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String ln = userDetail.get(Const.LOGINNAME);
        String dn = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("save tenantId=" + tenantId);
            log.debug("save roleId=" + role.getTenantId());
            log.debug("save roleName=" + role.getRoleName());
            log.debug("save roleDesc=" + role.getRoleDesc());
        }
        return rs.save(tenantId, ln, dn, role);
    }

    @RequestMapping(value="/api/v1/fa/role/{roleId}", method = RequestMethod.DELETE)
    public JSONObject delete(HttpServletRequest req, @PathVariable("roleId")String roleId,
                             @RequestParam(name="orgId",required = false)String orgId) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("delete tenantId=" + tenantId);
            log.debug("delete orgId=" + orgId);
            log.debug("delete roleId=" + roleId);
        }
        return rs.delete(tenantId, loginName, displayName, orgId, roleId);
    }

    @PostMapping(value="/api/v1/fa/role/checkRoleName",
            produces = "plain/text")
    public String checkRoleName(HttpServletRequest req,
                               @RequestParam(value="orgId", required=false)String orgId,
                               @RequestParam(value="roleId", required=false)String roleId,
                               @RequestParam("roleName")String roleName) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("checkPosName tenantId=" + tenantId);
            log.debug("checkPosName orgId=" + orgId);
            log.debug("checkPosName roleId=" + roleId);
            log.debug("checkPosName roleName=" + roleName);
        }
        return rs.checkAttr(tenantId, "RoleName", roleName, roleId);
    }

    @RequestMapping(value="/api/v1/fa/role/getGrant", method = RequestMethod.GET)
    public JSONArray saveGrant(HttpServletRequest req,
                                @RequestParam("roleId")String roleId,
                                @RequestParam("resControl")String resControl) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("saveGrant tenantId=" + tenantId);
            log.debug("saveGrant roleId=" + roleId);
            log.debug("saveGrant resControl=" + resControl);
        }
        JSONArray rtn = rs.getGrant(tenantId, roleId, resControl);
        return rtn;
    }

    @RequestMapping(value="/api/v1/fa/role/saveGrant", method = RequestMethod.POST)
    public JSONObject saveGrant(HttpServletRequest req,
                                @RequestParam("roleId")String roleId,
                                @RequestParam("resIds")String resIds,
                                @RequestParam("resControl")String resControl) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String ln = userDetail.get(Const.LOGINNAME);
        String dn = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("saveGrant tenantId=" + tenantId);
            log.debug("saveGrant loginName=" + ln);
            log.debug("saveGrant displayName=" + dn);
            log.debug("saveGrant roleId=" + roleId);
            log.debug("saveGrant resIds=" + resIds);
            log.debug("saveGrant resControl=" + resControl);
        }
        JSONObject rtn = rs.saveGrant(tenantId, ln, dn, roleId, resIds, resControl);
        return rtn;
    }

    @RequestMapping(value="/api/v1/fa/role/getMember", method = RequestMethod.GET)
    public JSONArray getMember(HttpServletRequest req,
                               @RequestParam("roleId")String roleId,
                               @RequestParam(name="memberType",required = false)String memberType) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        if (log.isDebugEnabled()) {
            log.debug("getMember tenantId=" + tenantId);
            log.debug("getMember roleId=" + roleId);
            log.debug("getMember memberType=" + memberType);
        }
        JSONArray rtn = rs.getMember(tenantId, roleId, memberType);
        return rtn;
    }

    @RequestMapping(value="/api/v1/fa/role/saveMember", method = RequestMethod.POST)
    public JSONObject saveMember(HttpServletRequest req,
                               @RequestParam("roleId")String roleId,
                               @RequestParam("members")String members) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        if (log.isDebugEnabled()) {
            log.debug("saveMember tenantId=" + tenantId);
            log.debug("saveMember roleId=" + roleId);
            log.debug("saveMember members=" + members);
        }
        return rs.saveMember(tenantId, loginName, displayName, roleId, members);
    }

    @RequestMapping(value = "/api/v1/fa/role/queryDhx", method = RequestMethod.POST)
    public String queryDhx(HttpServletRequest req,
                           @RequestParam("queryParams")String queryParams,
                        @RequestParam("fromRow")int fromRow,
                        @RequestParam("toRow")int toRow,
                        @RequestParam("timezoneOffset")int timezoneOffset,
                        @RequestParam("ordercolumns")String ordercolumns) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String tenantId = userDetail.get(Const.TENANT_ID);
        String lang = userDetail.get(Const.LANG);
        if (log.isDebugEnabled()) {
            log.debug("queryDhx queryParams=" + queryParams);
            log.debug("queryDhx tenantId=" + tenantId);
            log.debug("queryDhx lang=" + lang);
        }
        return rs.queryDhx(tenantId, queryParams, fromRow, toRow, timezoneOffset, lang, ordercolumns);
    }
}
